package action;

import com.opensymphony.xwork2.ActionSupport;
import model.Detail;
import model.Dish;
import model.Order;
import net.sf.json.JSONArray;
import service.DetailService;
import service.DishService;
import service.OrderService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by admin on 2016/8/30.
 */
public class MenuAction extends ActionSupport{
    private DishService dishService;
    private OrderService orderService;
    private JSONArray result;
    private List<Detail> data;
    private int receive;

    public void setDishService(DishService dishService) {
        this.dishService = dishService;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public JSONArray getResult() {
        return result;
    }

    public void setResult(JSONArray result) {
        this.result = result;
    }

    public String initMenu(){
        List<Dish> dishList = dishService.findAll();
        result = JSONArray.fromObject(dishList);
        return "result";
    }

    public String complete(){
        int total = 0;
        Order order = new Order();
        for(Detail d : data){
            d.setOrder(order);
            order.getDetails().add(d);
            total += d.getAmount() * dishService.findById(d.getDishId()).getPrice();
        }
        order.setTotal(total);
        order.setReceive(receive);
        order.setZhaoling(order.getReceive() - order.getTotal());
        order.setTime(new Date());
        orderService.saveOrder(order);
        return "success";
    }

    public void setData(List<Detail> data) {
        this.data = data;
    }

    public void setReceive(int receive) {
        this.receive = receive;
    }
}
